package com.feonix.common.view.grid;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.web.servlet.ModelAndView;

import com.feonix.common.permission.IPermissionCheckor;
import com.feonix.common.util.StringUtil;

public class GridView extends ModelAndView {

	protected String position;
	protected String id = "grid";

	protected String checkBoxField = "id";

	protected Boolean fitColumns = Boolean.valueOf(true);

	protected Boolean striped = Boolean.valueOf(true);

	protected Boolean nowrap = Boolean.valueOf(true);

	protected Boolean pagination = Boolean.valueOf(true);

	protected Boolean rownumbers = Boolean.valueOf(false);

	protected Boolean singleSelect = Boolean.valueOf(false);

	protected Boolean checkOnSelect = Boolean.valueOf(false);

	protected Boolean selectOnCheck = Boolean.valueOf(true);

	protected String pagePosition = "bottom";

	protected int pageNumber = 1;

	protected int pageSize = 15;

	protected String pageList = "10,15,20";

	protected int total = 0;

	protected String searchPlaceholder = "请输入关键字";

	protected String searchText = "";

	protected boolean showAdvSearch = true;

	protected boolean showSimpleSearch = true;

	protected int advDialogWidth = 400;

	protected int advDialogHeight = 300;
	protected String advDialogOptions;
	protected List<String> queryParams = new ArrayList<String>();

	protected List<Button> buttons = new ArrayList<Button>();

	protected List<Head> heads = new ArrayList<Head>();

	protected Boolean border = Boolean.valueOf(false);

	protected int searchType = 0;

	protected Boolean showPageList = Boolean.valueOf(true);
	protected String sortName;
	protected String sortOrder = "ASC";

	protected List<GridRow> datas = new ArrayList<GridRow>();

	private GridViewDelegate gridViewDelegate = null;

	public GridView() {
		super.addObject("grid", this);
	}

	public void addButton(Button button) {
		if (button.hasRight()){
			this.buttons.add(button);
		}
	}

	public void addButton(Button button, IPermissionCheckor<Button> checkor) {
		if (button != null)
			if (checkor == null) {
				addButton(button);
			} else {
				addButton(button, checkor.check(button));

			}
	}

	public void addButton(Button button, boolean hasPermission) {
		if ((button == null) || (!(hasPermission)))
			return;
		addButton(button);
	}

	public void addButton(Button button, Button[] subButtons) {
		addButton(button);
		if ((button.hasRight()) && (ArrayUtils.isNotEmpty(subButtons))){
			for (Button subButton : subButtons){
				if (subButton.hasRight()){
					button.addSubButton(subButton);
				}
			}
		}
	}

	public void addButton(Button button, Button[] subButtons, IPermissionCheckor<Button> checkor) {
		addButton(button, checkor);
		if ((button != null) && (ArrayUtils.isNotEmpty(subButtons))) {
			if (checkor == null) {
				button.getSubButtons().addAll(Arrays.asList(subButtons));
			} else {
				for (Button subButton : subButtons)
					if (checkor.check(button)) {
						button.addSubButton(subButton);
					}
			}
		}
	}

	public void addQueryParam(String key, Object value) {
		if ((!(StringUtil.isEmpty(key))) && (value != null)) {
			String str = StringUtil.getString(value);
			this.queryParams.add(key + ":'" + str + "'");
		}
		addObject(key, value);
	}

	public void addRow(GridRow row) {
		if (row != null)
			this.datas.add(row);
	}

	public void addHead(Head head) {
		if (head != null) {
			this.heads.add(head);
			if (head.getCheckbox().booleanValue())
				this.checkBoxField = head.getField();
		}
	}

	public void setFitColumns(Boolean fitColumns) {
		this.fitColumns = fitColumns;
	}

	public void setStriped(Boolean striped) {
		this.striped = striped;
	}

	public void setNowrap(Boolean nowrap) {
		this.nowrap = nowrap;
	}

	public void setPagination(Boolean pagination) {
		this.pagination = pagination;
	}

	public void setRownumbers(Boolean rownumbers) {
		this.rownumbers = rownumbers;
	}

	public void setSingleSelect(Boolean singleSelect) {
		this.singleSelect = singleSelect;
	}

	public void setPagePosition(String pagePosition) {
		this.pagePosition = pagePosition;
	}

	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setPageList(String pageList) {
		this.pageList = pageList;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public void setCheckOnSelect(Boolean checkOnSelect) {
		this.checkOnSelect = checkOnSelect;
	}

	public void setSelectOnCheck(Boolean selectOnCheck) {
		this.selectOnCheck = selectOnCheck;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setShowAdvSearch(boolean showAdvSearch) {
		this.showAdvSearch = showAdvSearch;
	}

	public void setSearchPlaceholder(String searchPlaceholder) {
		this.searchPlaceholder = searchPlaceholder;
	}

	public void setAdvDialogWidth(int advDialogWidth) {
		this.advDialogWidth = advDialogWidth;
	}

	public void setAdvDialogHeight(int advDialogHeight) {
		this.advDialogHeight = advDialogHeight;
	}

	public void setAdvDialogOptions(String advDialogOptions) {
		this.advDialogOptions = advDialogOptions;
	}

	public void setPosition(String position) {
		this.position = position;
		super.addObject("position", position);
	}

	public void setBorder(Boolean border) {
		this.border = border;
	}

	public int getPageNumber() {
		return this.pageNumber;
	}

	public int getPageSize() {
		return this.pageSize;
	}

	public String getSearchText() {
		return this.searchText;
	}

	public void setSearchText(String searchText) {
		this.searchText = searchText;
	}

	public GridViewDelegate getDelegate() {
		return this.gridViewDelegate;
	}

	public void setDelegate(GridViewDelegate gridViewDelegate) {
		this.gridViewDelegate = gridViewDelegate;
	}

	public void setSearchType(int searchType) {
		this.searchType = searchType;
	}

	public void setShowSimpleSearch(boolean showSimpleSearch) {
		this.showSimpleSearch = showSimpleSearch;
	}

	public void setShowPageList(Boolean showPageList) {
		this.showPageList = showPageList;
	}

	public String getSortName() {
		return this.sortName;
	}

	public String getSortOrder() {
		return this.sortOrder;
	}

	public void setSortName(String sortName) {
		this.sortName = sortName;
	}

	public void setSortOrder(String sortOrder) {
		this.sortOrder = sortOrder;
	}

}
